package com.erps.CommonUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class Sequence {
	private static Sequence instance = null;
	private static int iLastID = 0;
	private static int iMaxID = 0;
	private static String cDayTime = "";
	private static Random rnd = new Random();

	private Sequence() {

	}

	public static String AddMin(String cDay) {
		return cDay.replace("-", "") + "000000000000000000";
	}

	public static String AddMax(String cDay) {
		return cDay.replace("-", "") + "999999999999999999";
	}

	public static String DayNum(String cDay) {
		return cDay.replace("-", "");
	}

	public static Sequence getInstance() {
		if (instance == null)
			instance = new Sequence();
		return instance;
	}

	public long getMaxID() {
		java.util.Date dt = new Date();
		long lSysTime1 = dt.getTime();
		return lSysTime1;
	}

	public synchronized static String getGuid() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	public synchronized static String getID() {
		// 17+5+2+2
		// yyyyMMdd(8)
		// HHmmssSSS(9)
		// XXXXX(5)(0..99999)
		// XX(2)(0..99) 随机编码
		// XX(2)(0..99) 主机编码
		// (17+5+4)=26
		String cMachineID = Config.getMachineID();
		String cKeyID = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date curDate = new Date();
		String cDayStr = sdf.format(curDate);
		if (cDayStr.equals(cDayTime)) {
			iMaxID = iLastID + 1;
		} else {
			iMaxID = 0;
			cDayTime = cDayStr;
		}

		if (iMaxID <= 99999) {
			sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String cMaxID = String.format("%5d", iLastID).replace(" ", "0");
			String cRandom = String.format("%2d", rnd.nextInt(99)).replace(" ", "0");
			cKeyID = cDayStr + cMaxID + cRandom + cMachineID;
		}
		return cKeyID;
	}

	public synchronized static AutoID getAutoID(String cUrlTypeID) {
		int iUrlTypeID = StringEx.getInt(cUrlTypeID);
		return getAutoID(iUrlTypeID);
	}

	public static int getHashID(String cPageID) {
		String cKeyID = cPageID.substring(14, 17);
		int iKeyID = StringEx.getInt(cKeyID, 1);
		return (iKeyID % 12) + 1;
	}

	public synchronized static AutoID getAutoID(int iUrlTypeID) {
		// 17+5+2+2
		// yyyyMMdd(8)
		// HHmmssSSS(9)
		// XXXXX(5)(0..99999)
		// XX(2)(0..99) 类型
		// XX(2)(0..99) 主机编码
		// (17+5+4)=26
		String cMachineID = Config.getMachineID();
		AutoID vo = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date curDate = new Date();
		String cDayStr = sdf.format(curDate);
		if (cDayStr.equals(cDayTime)) {
			iLastID = iLastID + 1;
		} else {
			iLastID = 0;
			cDayTime = cDayStr;
		}

		if (iLastID <= 99999) {
			vo = new AutoID();
			sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String cMaxID = String.format("%5d", iLastID).replace(" ", "0");
			String cTypeID = String.format("%2d", iUrlTypeID).replace(" ", "0");
			vo.setID(cDayStr + cMaxID + cTypeID + cMachineID);
			vo.setUpdateDateTime(Long.valueOf(sdf.format(curDate)));
		}
		return vo;
	}

	public static void main(String[] arg) {
		System.out.println(getAutoID(11).ID);
		System.out.println(getAutoID(12).UpdateDateTime);
		System.out.println(getHashID("20160514225358345000001131"));

	}
}
